<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Config;
use think\Validate;
use think\Db;

/**
 * 剧情接口
 */
class Shortvideo extends Api
{
    protected $noNeedLogin = ['getBanner','getChannel','getHotRecommend'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }
    }


    /**
     * 短剧banner的列表接口（不分页）
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getBanner()
    {
        $where = [
            'is_allow' => 1,
            'is_banner' => 2
        ];

        $field = "id,name,image";
        $order = "weigh desc";

        $result = DB::name("shortvideo_script")->where($where)->order($order)->field($field)->select();
        $this->success('查询成功', $result);
    }

    /**
     * 短剧类目的列表接口（不分页）
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getChannel()
    {
        $field = "id,name,sort";
        $order = "sort desc";

        $result = DB::name("shortvideo_channel")->order($order)->field($field)->select();
        $this->success('查询成功', $result);
    }

    /**
     * 查询热门推荐的（分页）
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getHotRecommend()
    {
        $channel = input("channel_name");//类目名

        $page = input("page")?input("page"):1;
        $page_size = input("page_size")?input("page_size"):15;

        $where = [
            'is_allow' => 1,
        ];

        $whereRaw = "";
        if(!empty($channel)){
            $whereRaw.= "find_in_set('{$channel}',channel)";
        }

        $order = "weigh desc";

        $field = "id,name,image,status";

        $list = DB::name("shortvideo_script")->where($where)->where($whereRaw)->order($order)->page($page,$page_size)->field($field)->select();

        $this->success('查询成功', $list);
    }

    public function getCollection()
    {
        $result = [];

        $page = input("page")?input("page"):1;
        $page_size = input("page_size")?input("page_size"):15;

        if($this->auth->id){
            $user_id = $this->auth->id;
            $script_ids = DB::name("shortvideo_collection")->where("user_id",$user_id)->page($page,$page_size)->order("create_time DESC")->column('shortvideo_script_id');

            $field = "id,name,image,media_id,total_number";

            $list = DB::name("shortvideo_script")->whereIn('id',$script_ids)->field($field)->select();

            $result = $list;
        }

        $this->success('查询成功', $result);

    }



}
